package cn.tom.controller.adm;

import cn.tom.entity.Hot;
import cn.tom.service.HotService;
import cn.tom.service.LargeFileService;
import cn.tom.tool.XJson;
import cn.tom.tool.XResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RequestMapping({"/api/adm/hot"})
@RestController
public class HotController {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired(required = false)
    HotService hotService;
    @Autowired
    LargeFileService largeFileService;

    @PostMapping({"/add"})
    public XJson addHot(@RequestParam(value = "pic", required = false) MultipartFile file, Hot hot) throws IOException {
        Map<String, Object> map = new HashMap<>();
        this.log.info("file=" + file);
        this.log.info(hot.toString());
       /* if (file == null) {
            this.log.info("图片未上传 ");
        }
        if (file != null && file.getSize() > 0) {
            if (hot.getPicture() != null) {
                this.largeFileService.delete(hot.getPicture());
            }
            String uuid = UUID.randomUUID().toString().replace("-", "");
            hot.setPicture(uuid);
            this.largeFileService.add(uuid, file.getOriginalFilename(), file.getBytes());
        }
        if (hot.getId() > 0) {
            this.log.info("update=" + hot.toString());
            this.hotService.update(hot);
        } else {
            this.hotService.addHot(hot);
            this.log.info("add=" + hot.toString());
        }*/

        String fileName = "";
        String msg = "操作失败 .....";
        if (file != null) {
            fileName = file.getOriginalFilename();
        }
        System.out.println(fileName);
        if (hot.getId() == 0) {
            if (file != null && file.getSize() > 0) {
                System.out.println("如果有更新图片， 需要删除就的图片， 而后添加新的图片");
                String uuid = UUID.randomUUID().toString().replace("-", "");
                hot.setPicture(uuid);
                this.largeFileService.add(uuid, fileName, file.getBytes());
            }
            this.hotService.addHot(hot);
            msg = "添加本书成功...";
        }
        if ( hot.getId() > 0) {
            if (file != null && file.getSize() > 0) {
                System.out.println("删除旧的图片");
                this.largeFileService.delete(hot.getPicture());
                String uuid2 = UUID.randomUUID().toString().replace("-", "");
                hot.setPicture(uuid2);
                this.largeFileService.add(uuid2, fileName, file.getBytes());
            }
            msg = "更新成功...";
            this.hotService.update(hot);
        }
        map.put("hot", this.hotService.findById(hot.getId()));
        return XResp.pack(200, msg, map);
    }

    @GetMapping({"/findAll"})
    public XJson findAll(Integer type) {
        return XResp.pack(200, "OK", this.hotService.findAll());
    }

    @GetMapping({"/findById/{id}"})
    public XJson findById(@PathVariable("id") int id) {
        return XResp.pack(200, "OK", this.hotService.findById(id));
    }

    @GetMapping({"/remove/{id}"})
    public XJson delete(@PathVariable("id") int id) {
        Hot hot = this.hotService.findById(id);
        if (hot != null) {
            this.largeFileService.delete(hot.getPicture());
        }
        this.hotService.remove(id);
        return XResp.pack(200, "删除成功 ...", this.hotService.findAll());
    }
}
